<?php

namespace RTSCraft\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
  /* Generates the index page */
    public function indexAction()
    {
      $this->session();
      return $this->render('RTSCraftMainBundle:Default:index.html.twig');
    }

    public function styleAction()
    {
      $config = $this->getConfig();

      if ($config != NULL)
	{
	  $bg_img = $this->get('request')->getBasePath() . $config->getBgimg();
	  $response = 'body {background-image: url("' . $bg_img . '");}';
	  return new Response($response);
	}

      return new Response('');
    }

    public function logoAction($type)
    {
        $response = '';
	$config = $this->getConfig();

	if ($config != NULL)
	  {
	    switch ($type)
	      {
	      case 'logo':
                $response = $this->get('request')->getBasePath() . $config->getLogo();
		break;

	      case 'Rlogo':
		$response = $this->get('request')->getBasePath() . $config->getRlogo();
                break;

	      }
	    return new Response($response);
	  }
	return new Response('');
    }

    public function welcomeAction()
    {
      $config = $this->getConfig();
      $response = '<FIXME>Message de bienvenue</FIXME>';

      if ($config != NULL)
	{
	  $response = '<div class="welcome_msg">' . $config->getWelcomeMsg() . '</div>';
	}

	return new Response($response);
    }

    private function getConfig()
    {
        $rep = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Config');
        //again, 1 configuration only ...
        $configs = $rep->findAll();

	if (empty($configs))
	  {
	    return NULL;
	  }

        $config = reset($configs);
	return $config;

    }

    /* Generates session values if inexistant */
    private function session()
    {
      $session = $this->get("session");

      if (!$session->get("timeout"))
	{
	  $session->set("isAdmin", false);
	}
    }

}
